﻿
Imports std

Public Module share
    Private Function waitfor_indexedJob(ByVal job As indexedJob,
                                        ByVal queue As jobqueue) As Boolean
        assert(Not job Is Nothing)
        assert(Not queue Is Nothing)
        Return waitfor(job.end) AndAlso
               queue.push(job)
    End Function

    Private Function indexedJobStep(ByVal job As indexedJob,
                                    ByVal queue As jobqueue,
                                    ByVal [goto] As _do(Of Boolean)) As _do(Of Boolean)
        assert(Not [goto] Is Nothing)
        Return Function() As Boolean
                   Return waitfor_indexedJob(job, queue) AndAlso
                          [goto]()
               End Function
    End Function

    'TODO: retire ijobEventComb
    Public Function waitfor_ijob(ByVal job As ijob,
                                 ByVal queue As jobqueue,
                                 Optional ByRef indexedJob As indexedJob = Nothing) As Boolean
        assert(Not job Is Nothing)
        assert(Not queue Is Nothing)
        indexedJob = queue.nextJob(job)
        Return waitfor_indexedJob(indexedJob, queue)
    End Function

    Public Function ijobStep(ByVal job As ijob,
                             ByVal queue As jobqueue,
                             ByVal [goto] As _do(Of Boolean),
                             Optional ByRef indexedJob As indexedJob = Nothing) As _do(Of Boolean)
        assert(Not job Is Nothing)
        assert(Not queue Is Nothing)
        indexedJob = queue.nextJob(job)
        Return indexedJobStep(indexedJob, queue, [goto])
    End Function

    Public Function ijobEventComb(ByVal job As ijob,
                                  ByVal queue As jobqueue,
                                  Optional ByRef indexedJob As indexedJob = Nothing) As eventComb
        Return New eventComb(ijobStep(job, queue, AddressOf goto_end, indexedJob))
    End Function

    Private _planetQueueName As String = Nothing
    Public Function planetQueueName() As String
        If _planetQueueName Is Nothing Then
            _planetQueueName = strtolower(machinename() + character.plusSign + envs.serviceName())
        End If
        Return _planetQueueName
    End Function

    Public Sub threadpoolRun(ByVal v As void)
        If Not v Is Nothing Then
            resolve(Of ithreadpool)().insertJob(Sub(_NIU_ As Object)
                                                    v()
                                                End Sub, Nothing)
        End If
    End Sub

    Public Function read_port_connectionCount(ByVal config As configParser,
                                              ByVal name As String,
                                              ByRef port As Int32,
                                              ByRef connectionCount As Int64,
                                              ByVal defaultConnectionCount As Int64) As Boolean
        If config.getparameter(name, "port", port) Then
            connectionCount = config.parameter(name, "connectionCount", defaultConnectionCount)
            Return True
        Else
            raiseError(errorHandle.errorType.user, "no port has been set in ", name)
            Return False
        End If
    End Function
End Module
